setwd('~/cogsci/projects/eye_tracking_code/et-ana/')
load("rdata/dt.RData")

# choose early or late subset
early = subset(dt, POD == "early")
late = subset(dt, POD == "late")
######################################
# Step 1. Get all the first saccades
######################################
delay.early = getFirstSaccades(d=early, timevar="Time_rel_stim_Qonset", eyeeventvar="Right_Type", trialdataidvar="TrialDataID", dataidvar="DataID")
nrow(delay.early)

delay.late = getFirstSaccades(d=late, timevar="Time_rel_stim_Qonset", eyeeventvar="Right_Type", trialdataidvar="TrialDataID", dataidvar="DataID")
nrow(delay.late)

######################################
# Step 2. Exclude trials based on SKT criteria
######################################
delay.early = excludeTrials(d=early,d.sacc=delay.early,timevar="Time_rel_stim_Qonset", trialdataidvar="TrialDataID", dataidvar="DataID",regionvar="rp_RegionType")

delay.late = excludeTrials(d=late,d.sacc=delay.late,timevar="Time_rel_stim_Qonset", trialdataidvar="TrialDataID", dataidvar="DataID",regionvar="rp_RegionType")

## if you want to remove trials where subject did NOT click on target
#table(delay$ResponseType)
#delay=subset(delay,delay$ResponseType=="t")

########################################## 
# Step 3. Generate saccade histograms for each condition
########################################## 
# set delay 
delay = delay.early # or
delay = delay.late

# size of bins in histograms
HISTO_BINWIDTH = 40  # try other bindwidths too

delay$Region = delay$rp_RegionType
delay$Time = delay$Time_rel_stim_Qonset

p = ggplot(delay, aes(x = Time, fill = Region)) +
  geom_histogram(position = "identity", alpha = .5, binwidth = HISTO_BINWIDTH) +
  xlim(0, 800) +
  facet_grid(Quantifier~Numbers) +
  xlab("Time since quantifier onset (ms)") + ylab("Number of saccades") +
  theme_bw()
p


######################################
## Step 4. Moving window analysis to determine first time window in which probability of saccade to target is different from probability of saccade to competitor
######################################

chi_critical = 3.841 # two-tailed (?)
chi_marginal = 2.706

BIN_SIZE    = 60 # play with bin sizes
WINDOW_SIZE = 20

# small and big set analysis combined
resrep = length(seq(0, 660, by = WINDOW_SIZE))*2*2

results = data.frame(Onset = rep(-1, resrep), Offset = rep(-1, resrep), competitor = rep(-1, resrep), target = rep(-1, resrep), statistic = rep(-1, resrep), p_value = rep(-1, resrep), numbers = factor(x=rep(NA, resrep),levels=c("absent","present")), quantifier = factor(x=rep(NA, resrep),levels=c("all","some")))

i = 0

for (count in seq(0, 660, by = WINDOW_SIZE))
{
  for (n in levels(results$numbers))
  {
    for (q in levels(results$quantifier))
    {
      i = i + 1
      x = table(subset(delay, Numbers == n & Quantifier == q & Time_rel_stim_Qonset >= count & Time_rel_stim_Qonset < count + BIN_SIZE)$rp_RegionType)
      y = c(x[[1]], x[[2]])
      c2.test = chisq.test(y)
      results[i, "Onset"] = count
      results[i, "Offset"] = count + BIN_SIZE
      results[i, "competitor"] = x[[1]]
      results[i, "target"] = x[[2]]
      results[i, "statistic"] = round(c2.test$statistic, 2)
      results[i, "p_value"] = round(c2.test$p.value, 3)
      results[i, "numbers"] = n
      results[i, "quantifier"] = q
    }
  }
}

# Plot moving window chi-squared tests. Where does chi-squared first become significant in each condition? What are potential problems of looking at the data this way?
p = ggplot(results,aes(x=Onset, y=statistic)) +
  geom_point() +
  xlab("Onset of time bin, relative to quantifier onset (ms)") +
  ylab("Chi-square statistic") +
  facet_grid(quantifier~numbers) +
  geom_hline(yintercept = chi_critical, color = "black") +
  geom_hline(yintercept = chi_marginal, color = "gray80")
p